package com.leet.code.lcof;

import com.algorithm.model.TreeNode;

import java.util.LinkedList;
import java.util.List;

public class Code32_2 {
    public static void main(String[] args) {

    }

    public List<List<Integer>> levelOrder(TreeNode root) {

        LinkedList<List<Integer>> rsList = new LinkedList<>();

        if (root == null) {
            return rsList;
        }

        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        treeNodes.add(root);

        while (treeNodes.size() != 0) {
            LinkedList<Integer> linkedList = new LinkedList<>();
            LinkedList<TreeNode> linkedListNext = new LinkedList<>();
            for (TreeNode treeNode : treeNodes) {
                linkedList.add(treeNode.val);
                if (treeNode.left != null) {
                    linkedListNext.addLast(treeNode.left);
                }
                if (treeNode.right != null) {
                    linkedListNext.addLast(treeNode.right);
                }
            }
            rsList.addLast(linkedList);
            treeNodes = linkedListNext;
        }

        return rsList;

    }


}
